package sc.love.boot.es;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class run {

	private final int MaxHandingNum = 24;// 最大处理数

	public void go() {
		Connection connection = null;
		Statement statement = null;
		ResultSet executeQuery = null;
		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(32);
		try {
			connection = _getConnection();
			statement = connection.createStatement();
			executeQuery = statement.executeQuery("select count(1) count from message a ");
			while (executeQuery.next()) {
				int count = executeQuery.getInt("count");
				if (count > MaxHandingNum) {
					// 超过最大处理数,取模进行多线程
					int batch = count % MaxHandingNum == 0 ? count / MaxHandingNum : count / MaxHandingNum + 1;
					for (int i = 0; i < batch; i++) {
						int start = i * MaxHandingNum;
						int end = (i + 1) * MaxHandingNum;
						MultithreadUploadMessage multithreadUpload = new MultithreadUploadMessage(start, end, connection);
						newFixedThreadPool.execute(multithreadUpload);
					}
					System.out.println(batch);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// try {
			// executeQuery.close();
			// statement.close();
			// connection.close();
			// } catch (SQLException e) {
			// e.printStackTrace();
			// }
		}

	}
	public static void main(String[] args) {
		//new run().go();
		int batch = 5 / 2 + 1;
		System.out.println(batch);
	}

	public static Connection _getConnection() {
		//sid jdbc:oracle:thin:@136.160.40.38:8341:instpdb
		//serviceName  jdbc:oracle:thin:@//136.160.40.38:8341/instpdb
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");// 注册驱动
			Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@//127.0.0.1:1621/msgct1_mcdb", "MSGCENTER",
					"B8^As$pX-t");
			return connection;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}
}
